package com.heima.tanhua.serivce.impl;

import cn.hutool.core.util.IdUtil;
import com.heima.tanhua.commonResult.Result;
import com.heima.tanhua.dto.LikerAddDto;
import com.heima.tanhua.pojo.Follow;
import com.heima.tanhua.pojo.User;
import com.heima.tanhua.mapper.FollowMapper;
import com.heima.tanhua.mapper.UserMapper;
import com.heima.tanhua.serivce.UserLikeService;
import com.heima.tanhua.thread.UserThreadLocal;
import com.heima.tanhua.vo.LikeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserLikeServiceImpl implements UserLikeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FollowMapper followMapper;

    @Override
    public Boolean addList(LikerAddDto dto) {
        if (dto == null || StringUtils.isEmpty(dto.getUserId())) {
            throw new RuntimeException("入参不能为空");
        }
        String followId = dto.getUserId();
        String userId = UserThreadLocal.get();

        User followUser = userMapper.selectById(followId);
        if (followUser == null) {
            throw new RuntimeException("被喜欢者不存在");
        }
        Follow follow = new Follow();
        follow.setId(IdUtil.getSnowflakeNextIdStr());
        follow.setUserId(userId);
        follow.setFollowId(followId);
        follow.setCreateTime(LocalDateTime.now());
        Boolean res = followMapper.insertOne(follow);
        return res;
    }

    @Override
    public boolean removeLike(String followId) {
        if (StringUtils.isEmpty(followId)) {
            throw new RuntimeException("入参不能为空");
        }
        String userId = UserThreadLocal.get();
        Follow follow = followMapper.selectByUserIdAndFollowId(userId, followId);
        if (follow == null) {
            log.warn("关系id不存在");
            return true;
        }

        return followMapper.deleteByUserIdAndFollowId(userId, followId);
    }

    @Override
    public Result getLikeList(String name, Integer type, Integer page, Integer pageSize) {
        page = (page - 1) * pageSize;
        String userId = UserThreadLocal.get();
        List<LikeVo> list;
        Long total;
        if (type == 1) {
            // 查互相关注
            list = followMapper.selectByType1(userId, name, page, pageSize);
            for (LikeVo likeVo : list) {
                likeVo.setUserId(userId);
            }
            total=followMapper.selectCountByType1(userId,name);
        }
        if (type == 2) {
            // 查喜欢
            list = followMapper.selectByType2(userId, name, page, pageSize);
            for (LikeVo likeVo : list) {
                likeVo.setUserId(userId);
            }
            total=followMapper.selectCountByType2(userId,name);
        } else {
            // 查粉丝
            list = followMapper.selectByType3(userId, name, page, pageSize);
            for (LikeVo likeVo : list) {
                likeVo.setUserId(userId);
            }
            total= followMapper.selectCountByType3(userId,name);
        }
        return Result.list(list,total);
    }
}
